#include <stdio.h>
#include <string.h>

int getSum(char data[], int number)
{
	int sum = 0;
	for(int i = 0; i < strlen(data); i++){
		if(number == (data[i] - '0')){
			sum = sum * 10 + number;
		}
	}
	return sum;	
}
int main()
{
	int A = 0, B = 0;
	char input[2][1024];
	int sum = 0;
	scanf("%s %d %s %d", &input[0], &A, &input[1], &B);
	sum += getSum(input[0], A);
	sum += getSum(input[1], B);
	printf("%d\n", sum);
	return 0;
}
